/**
 ************************************************************
 * @file         gizwits_product.c
 * @brief        Gizwits 控制协议处理,及平台相关的硬件初始化 
 * @author       Gizwits
 * @date         2016-09-05
 * @version      V03010201
 * @copyright    Gizwits
 * 
 * @note         机智云.只为智能硬件而生
 *               Gizwits Smart Cloud  for Smart Products
 *               链接|增值ֵ|开放|中立|安全|自有|自由|生态
 *               www.gizwits.com
 *
 ***********************************************************/
#include <stdio.h>
#include <string.h>
#include "gizwits_protocol.h"
#include "driver/hal_LED.h"
#include "config.h"
/** 用户区当前设备状态结构体*/
extern dataPoint_t currentDataPoint;
extern system_info_t systemInfo;
extern gdata_t led_data; 
extern COLOR_t color_arr[48];
/**@name Gizwits 用户API接口
 * @{
 */
static unsigned int color_7_mode_speed[] = {500,  1000,  2000, 2500, 3000,  3500, 4000, 4500, 5000, 5500};
static unsigned char color_7_mode_table[7] = {0, 8, 16,  25,  30, 38, 46};
void ICACHE_FLASH_ATTR color_7_mode_callback()
{
      static unsigned int local_count = 0;
      unsigned char tmp = color_7_mode_table[local_count];

      pen_color2(color_arr[tmp].R,color_arr[tmp].G,color_arr[tmp].B,color_arr[tmp].W);

      //os_printf("test speed !!! waning, please delete this code after test\r\n");
	
      local_count ++;
      if (local_count == 7)
         local_count = 0;
}


extern void breathingLightStart(unsigned char delay);

/**
 * @brief 事件处理接口

 * 说明：

 * 1.用户可以对WiFi模组状态的变化进行自定义的处理

 * 2.用户可以在该函数内添加数据点事件处理逻辑，如调用相关硬件外设的操作接口

 * @param[in] info : 事件队列
 * @param[in] data : 协议数据
 * @param[in] len : 协议数据长度
 * @return NULL
 * @ref gizwits_protocol.h
 */
int8_t ICACHE_FLASH_ATTR gizwitsEventProcess(eventInfo_t *info, uint8_t *data, uint32_t len)
{
	uint8_t i = 0;
	uint8_t rgbFlag = 0; 
	dataPoint_t *dataPointPtr = (dataPoint_t *)data;
	moduleStatusInfo_t *wifiData = (moduleStatusInfo_t *)data;
	LOCAL os_timer_t color_7_mode_timer;

	if((NULL == info) || (NULL == data))
	{
		return -1;
	}

	for(i=0; i<info->num; i++)
	{
		switch(info->event[i])
		{
			case EVENT_ONOFF:
				currentDataPoint.valueOnOff = dataPointPtr->valueOnOff;
				os_printf("Evt: EVENT_ONOFF %d \n", currentDataPoint.valueOnOff);
				
				systemInfo.devStatus.Isonoff = currentDataPoint.valueOnOff;
				configWriteParam();

				if(0x01 == currentDataPoint.valueOnOff)
				{
					//user handle
				}
				else
				{
					//user handle    
				}
				break;

			case EVENT_REG:
				currentDataPoint.valueREG = dataPointPtr->valueREG;
				//os_printf("Evt:EVENT_REG %d\n", currentDataPoint.valueREG); 
				led_data.RED = currentDataPoint.valueREG;
				systemInfo.devStatus.r = led_data.RED;
				os_printf("Evt:led_data.RED %d \n",led_data.RED);
				break;

			case EVENT_GREEN:
				currentDataPoint.valueGREEN = dataPointPtr->valueGREEN;
				//os_printf("Evt:EVENT_GREEN %d\n", currentDataPoint.valueGREEN); 
				led_data.GREEN = currentDataPoint.valueGREEN; 
   				systemInfo.devStatus.g = led_data.GREEN;
				os_printf("Evt:led_data.GREEN %d \n", led_data.GREEN); 
				break;

			case EVENT_BLUE:
				currentDataPoint.valueBLUE = dataPointPtr->valueBLUE;
				//os_printf("Evt:EVENT_BLUE %d\n", currentDataPoint.valueBLUE); 
   				systemInfo.devStatus.b = led_data.BLUE;
				led_data.BLUE = currentDataPoint.valueBLUE; 
				os_printf("Evt:led_data.BLUE %d \n", led_data.BLUE); 
				break;

			case EVENT_COLORVALUE:
				currentDataPoint.valueCOLORVALUE = dataPointPtr->valueCOLORVALUE;
				os_printf("Evt:EVENT_COLORVALUE %d\n",currentDataPoint.valueCOLORVALUE);
				if (currentDataPoint.valueOnOff)
				{
					LED_Color_Value(currentDataPoint.valueCOLORVALUE); 
					rgbFlag = 1;
				}
				else
					rgbFlag = 0;
#if 0
				float bright = 0;
				float temp = 0;
				unsigned char tmp_red;
				unsigned char tmp_green;
				unsigned char tmp_blue;
				unsigned char tmp_white; 
				unsigned char color_value = currentDataPoint.valueCOLORVALUE; 

				if((0 <= color_value) && (color_value < 48))
				{
					os_printf( "we get in =%d\r\n", color_value); 
					led_data.COLOR_VAL = currentDataPoint.valueCOLORVALUE; 
					led_data.RED = color_arr[led_data.COLOR_VAL].R;
					led_data.GREEN = color_arr[led_data.COLOR_VAL].G;
					led_data.BLUE = color_arr[led_data.COLOR_VAL].B;

					led_data.WHITE = color_arr[led_data.COLOR_VAL].W;
					bright = (float)led_data.COLOR_BRIGHT / 100.0;
					temp = bright * (float)led_data.RED;
					tmp_red = (unsigned char)temp;
					temp = bright * (float)led_data.GREEN;
					tmp_green = (unsigned char)temp;
					temp = bright * (float)led_data.BLUE;
					tmp_blue = (unsigned char)temp;
					ledColor(tmp_red, tmp_green, tmp_blue, 0); 
					led_data.RED = tmp_red;
					led_data.GREEN = tmp_green;
					led_data.BLUE = tmp_blue;
				}
				else if(color_value >= 200 && color_value < 210)
				{
					led_data.COLOR_VAL = pTxBuf->phead[7];
					//bright = 1;//(float)led_data.COLOR_BRIGHT / 100.0;
					tmp_red = color_temp_arr[color_value - 200].R;
					//tmp_red= (unsigned char )temp;
					tmp_green = color_temp_arr[color_value - 200].G;

					tmp_blue = color_temp_arr[color_value - 200].B;

					ledColor(tmp_red, tmp_green, tmp_blue, color_temp_arr[color_value - 200].W); 

					led_data.RED = tmp_red;
					led_data.GREEN = tmp_green;
					led_data.BLUE = tmp_blue;
					led_data.WHITE = color_temp_arr[color_value - 200].W;
				}
#endif
				break;
			case EVENT_COLORBRIGHT:
				currentDataPoint.valueCOLORBRIGHT = dataPointPtr->valueCOLORBRIGHT;
				os_printf("Evt:EVENT_COLORBRIGHT %d\n",currentDataPoint.valueCOLORBRIGHT);
				if (currentDataPoint.valueOnOff)
				{
				    LED_Color_Bright(currentDataPoint.valueCOLORBRIGHT); 
				    rgbFlag = 1; 
				}
				else
				    rgbFlag = 0;
				break;

			case EVENT_UNUSED:
				os_printf("Evt: EVENT_UNUSED\n");
				memcpy((uint8_t *)&currentDataPoint.valueUNUSED,(uint8_t *)&dataPointPtr->valueUNUSED,sizeof(currentDataPoint.valueUNUSED));

				if (currentDataPoint.valueUNUSED[0] == 0x1)
				{
					if (currentDataPoint.valueUNUSED[1]>=0 && currentDataPoint.valueUNUSED[1]<10 )
					{
						os_timer_disarm(&color_7_mode_timer);
						os_timer_setfn(&color_7_mode_timer, (os_timer_func_t *)color_7_mode_callback , NULL);
						os_timer_arm(&color_7_mode_timer, color_7_mode_speed[currentDataPoint.valueUNUSED[1]], 1);
					}
				}
				else if (currentDataPoint.valueUNUSED[0] == 0x0)
				{
					os_timer_disarm(&color_7_mode_timer);
				}

				//user handle
				break;
			case EVENT_TIMER:
				os_printf("Evt: EVENT_TIMER\n");
				memcpy((uint8_t *)&currentDataPoint.valueTIMER,(uint8_t *)&dataPointPtr->valueTIMER,sizeof(currentDataPoint.valueTIMER));
				//user handle
				break;
			case EVENT_SOFT_VER:
				os_printf("Evt: EVENT_SOFT_VER\n");
				memcpy((uint8_t *)&currentDataPoint.valueSOFT_VER,(uint8_t *)&dataPointPtr->valueSOFT_VER,sizeof(currentDataPoint.valueSOFT_VER));
				//user handle
				break;
			case EVENT_TIMER_COL_VAL:
				os_printf("Evt: EVENT_TIMER_COL_VAL\n");
				memcpy((uint8_t *)&currentDataPoint.valueTIMER_COL_VAL,(uint8_t *)&dataPointPtr->valueTIMER_COL_VAL,sizeof(currentDataPoint.valueTIMER_COL_VAL));
				//user handle
				break;

			case WIFI_SOFTAP:
				breathingLightSetEffect(1);
				breathingLightStart(30);
				break;
			case WIFI_AIRLINK:
				breathingLightSetEffect(0);
				breathingLightStart(5); 
				break;
			case WIFI_STATION:
				break;
			case WIFI_CON_ROUTER:
				breathingLightStop(); 
				os_printf("@@@@ W2M->WIFI_CONNROUTER \n"); 
				if (systemInfo.ISCONFIG == 0x0)
				{
				    systemInfo.ISCONFIG = 0x1;
			   	    configWriteParam();
				    LED_Color_Value(207);    		
				}
				break;
			case WIFI_DISCON_ROUTER:
				break;
			case WIFI_CON_M2M:
				break;
			case WIFI_DISCON_M2M:
				break;
			case WIFI_RSSI:
				os_printf("RSSI %d\n", wifiData->rssi);
				break;
			case TRANSPARENT_DATA:
				os_printf("TRANSPARENT_DATA \n");
				//user handle , Fetch data from [data] , size is [len]
				break;
			default:
				break;
		}
	}

	


	if(rgbFlag != 1)
	{
		if(0x01 == currentDataPoint.valueOnOff) 
		{
			os_printf("status:[ON] \n"); 
			ledColor(systemInfo.devStatus.r, systemInfo.devStatus.g, systemInfo.devStatus.b, systemInfo.devStatus.w); 

			os_printf("[ R : %d, G : %d, B : %d, w : %d] \n", systemInfo.devStatus.r, systemInfo.devStatus.g, systemInfo.devStatus.b, systemInfo.devStatus.w); 
		}
		else if(0x00 == currentDataPoint.valueOnOff) 
		{
			os_printf("status:[OFF] \n"); 

			ledColor(0, 0, 0, 0); 

			os_printf("[ R : %d, G : %d, B : %d, w : %d] \n", led_data.RED, led_data.GREEN, led_data.BLUE, led_data.WHITE); 
		}
	}

	system_os_post(USER_TASK_PRIO_0, SIG_UPGRADE_DATA, 0);
	return 0;
}
/**@} */
